//
//  SettingsView.m
//  Tryton-Mobile-App
//
//  Created by Angelo Conconi on 26.05.11.
//  Copyright 2011 Siemens Building Technologies. All rights reserved.
//

#import "SettingsView.h"
#define FILE_PATH [NSHomeDirectory() stringByAppendingPathComponent:@"UserSettings.txt"]



@implementation SettingsView
@synthesize Username, Password, Serverpath, Port, Database, scrollView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [Password release];
    [Serverpath release];
    [Port release];
    [Database release];
    [Username release];
    [scrollView release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    scrollView.contentSize = self.view.frame.size;
    // Do any additional setup after loading the view from its nib.
    BOOL fileExists =  [[NSFileManager defaultManager] fileExistsAtPath:FILE_PATH];
    if (fileExists) {
        NSString * LoadedUsername = [NSString stringWithContentsOfFile:FILE_PATH encoding:NSUTF8StringEncoding error:nil];
        Username.text = LoadedUsername;
    }
    
    //Save Button
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(saveButtonClicked:)] autorelease];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
    keyboardVisible = NO;
    
}

- (void)viewWillDisappear:(BOOL)animated 
{ 
    NSLog(@"Unregistering for keyboard events");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardDidShow:(NSNotification *)notif
{ 
	NSLog(@"Received UIKeyboardDidShowNotification.");
    
    if (keyboardVisible)
    {
        NSLog(@"Keyboard is already visible. Ignoring notification.");
        return;
    }
    // The keyboard wasn't visible before
    NSLog(@"Resizing smaller for keyboard");
    
    // Get the size of the keyboard
    NSDictionary* info = [notif userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    // Resize the scroll view to make room for the keyboard
    CGRect viewFrame = self.view.frame;
    viewFrame.size.height -= keyboardSize.height;
    scrollView.frame = viewFrame;
    keyboardVisible = YES;
    

}

- (void)keyboardDidHide:(NSNotification *)notif
{
    NSLog(@"Received UIKeyboardDidHideNotification.");
    
    if (!keyboardVisible)
    {
        NSLog(@"Keyboard is already hidden. Ignoring notification.");
        return;
    }
    // The keyboard was visible
    NSLog(@"Resizing bigger with no keyboard");
    
    // Get the size of the keyboard
    NSDictionary* info = [notif userInfo];
    NSValue* aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
    CGSize keyboardSize = [aValue CGRectValue].size;
    
    // Reset the height of the scroll view to its original value
    CGRect viewFrame = self.view.frame;
    viewFrame.size.height += keyboardSize.height;
    scrollView.frame = viewFrame;
    keyboardVisible = NO;
}

- (void)viewDidUnload 
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(IBAction) saveButtonClicked:(id) sender{
	NSLog(@"Save pressed!");
    NSString *UsernameToSave = [Username text];
    [UsernameToSave writeToFile:FILE_PATH atomically:YES encoding:NSUTF8StringEncoding error:nil];
   	NSLog(@"Value of Username Textfield: %@", [Username text]);
	[self dismissModalViewControllerAnimated:YES];
}

@end
